import { _decorator, Component, Node } from 'cc';
import { NodePool } from '../../../../sgame/pool/NodePool';
import { UIDialog } from '../dialog/UIDialog';
import { DialogOption } from '../../../../sgame/ui/SUI';
const { ccclass, property } = _decorator;

@ccclass('UIDialogLayer')
export class UIDialogLayer extends Component {
    @property(NodePool)
    public pool: NodePool;

    public show(text: string | DialogOption, onConfirm?: () => void, onCancel?: () => void, yesBtnTxt?: string, noBtnTxt?: string): void {
        if (typeof text !== "string") {
            onConfirm = text.onConfirm;
            onCancel = text.onCancel;
            yesBtnTxt = text.yesBtnTxt;
            noBtnTxt = text.noBtnTxt;
            text = text.text;
        }

        let dialogNode: Node = this.pool.get();
        let dialog = dialogNode.getComponent(UIDialog);
        dialog.onHide = this.hide.bind(this);
        dialog.show(text, onConfirm, onCancel, yesBtnTxt, noBtnTxt);
    }

    public hide(dialogNode: Node): void {
        this.pool.put(dialogNode);
    }
}